// Copyright (c) 2022 CHANGLEI. All rights reserved.

import 'dart:io';

import 'package:aircraft_war/bloc/game_state_cubit.dart';
import 'package:aircraft_war/commons/fonts.dart';
import 'package:aircraft_war/components/main_game.dart';
import 'package:aircraft_war/foundation/sounds.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_grasp/flutter_grasp.dart';

const _buttonDecoration = ShapeDecoration(
  shape: StadiumBorder(
    side: BorderSide(
      color: CupertinoColors.label,
      width: 1,
    ),
  ),
);
const _buttonTextStyle = TextStyle(
  color: CupertinoColors.label,
  fontFamily: handwritten,
  fontSize: 18,
);
const _buttonPadding = EdgeInsets.symmetric(
  horizontal: 40,
);
const _buttonMinSize = 32.0;

/// Created by changlei on 2022/5/18.
///
/// 游戏操作面板
class OperationPanel extends StatefulWidget {
  /// 游戏操作面板
  const OperationPanel({
    Key? key,
    required this.game,
  }) : super(key: key);

  /// 游戏实体
  final MainGame game;

  @override
  State<OperationPanel> createState() => _OperationPanelState();
}

class _OperationPanelState extends State<OperationPanel> {
  @override
  Widget build(BuildContext context) {
    final state = widget.game.stateCubit.state;
    return SafeArea(
      child: Align(
        alignment: const Alignment(0.0, 0.02),
        child: Container(
          padding: const EdgeInsets.symmetric(
            vertical: 20,
            horizontal: 40,
          ),
          decoration: BoxDecoration(
            color: const Color(0xEEBCC1C2),
            borderRadius: BorderRadius.circular(10),
            border: Border.all(
              color: CupertinoColors.label,
              width: 1,
            ),
          ),
          child: IntrinsicWidth(
            child: WidgetGroup.spacing(
              mainAxisSize: MainAxisSize.min,
              direction: Axis.vertical,
              spacing: 16,
              children: [
                if (state == GameState.idle || state == GameState.pending)
                  _Button(
                    text: '开始游戏',
                    onPressed: () {
                      widget.game.aliveCubit.reset();
                      widget.game.scoreCubit.reset();
                      widget.game.bulletCubit.reset();
                      widget.game.firepowerCubit.reset();
                      widget.game.stateCubit.run();
                    },
                  ),
                if (state == GameState.pausing)
                  _Button(
                    text: '继续游戏',
                    onPressed: () {
                      widget.game.stateCubit.run();
                    },
                  ),
                if (state == GameState.pausing)
                  _Button(
                    text: '结束游戏',
                    onPressed: () {
                      widget.game.stateCubit.pending();
                    },
                  ),
                if (state == GameState.stopped)
                  _Button(
                    text: '重新开始',
                    onPressed: () {
                      widget.game.stateCubit.pending();
                    },
                  ),
                if (state == GameState.pending || state == GameState.pending || state == GameState.stopped)
                  _Button(
                    text: '历史成绩',
                    onPressed: () {},
                  ),
                _Button(
                  text: '${Sounds.isMute ? '打开' : '关闭'}音效',
                  onPressed: () {
                    setState(() {
                      Sounds.mute(!Sounds.isMute);
                    });
                  },
                ),
                _Button(
                  text: '退出游戏',
                  onPressed: () {
                    exit(0);
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _Button extends StatelessWidget {
  const _Button({
    Key? key,
    required this.text,
    this.onPressed,
  }) : super(key: key);

  final String text;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: _buttonDecoration,
      child: CupertinoButton(
        minSize: _buttonMinSize,
        padding: _buttonPadding,
        onPressed: () {
          Sounds.button();
          onPressed?.call();
        },
        child: Text(
          text,
          style: _buttonTextStyle,
        ),
      ),
    );
  }
}
